# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/2/15
# @File   : R26_IC_NonExternalGuaranteeSpecialTransactionTypeSixtyMonsCheck.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:申请人5年内非对外担保业务校验
code:R26_IC_NonExternalGuaranteeSpecialTransactionTypeSixtyMonsCheck
tableName:
columnName:
groups:个人征信校验
dependencies:IC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime
from dateutil.relativedelta import relativedelta

reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = IC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['reportNo'] in null_type_list:
                return [{}]
            # 存在空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def isfloat(x):

    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def formatDate(time):
    try:
        import re
        import datetime
        r1 = re.compile(u'年|月|日')
        time = r1.sub('.', time)
        try:
            date = datetime.datetime.strptime(time, '%Y.%m.%d').strftime('%Y%m%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y-%m-%d').strftime('%Y%m%d')
            except:
                try:
                    date = datetime.datetime.strptime(time, '%Y/%m/%d').strftime('%Y%m%d')
                except:
                    try:
                        r = re.compile(r'\D+')
                        tmp = r.sub('', time)
                        if len(tmp) > 8:
                            tmp = tmp[:8]
                        date = datetime.datetime.strptime(tmp, '%Y%m%d').strftime('%Y%m%d')
                    except:
                        date = None
        return date
    except:
        return None

def specialTrade(tabelName):
    table = getTable(table_Name=tabelName)
    Factor = 0
    flag = []
    if table is None:
        return None
    elif table == [{}]:
        return 0
    else:
        five_years_ago = (datetime.datetime.now() - relativedelta(years=5)).strftime("%Y%m%d")
        for i in table:
            try:
                if i['tradeType'] in [u'担保人代还', u'以资抵债', u'展期', u'延期']:
                    if i['getTime'] not in null_type_list and formatDate(i['getTime']) is not None:
                        if formatDate(i['getTime']) > five_years_ago:
                            Factor += 1
                    else:
                        flag.append(None)
                        continue
            except:
                flag.append(None)
                continue
        if len(flag) == len(table):
            return None
        else:
            return Factor

def R26_IC_NonExternalGuaranteeSpecialTransactionTypeSixtyMonsCheck():
    Factor1 = specialTrade('ICRSpecialTrade*Loan')
    Factor2 = specialTrade('ICRSpecialTrade*Card')
    Factor3 = specialTrade('ICRSpecialTrade*Std')
    if Factor1 is None and Factor2 is None and Factor3 is None:
        return u'缺失值'
    else:
        if Factor1 is None:
            Factor1 = 0
        if Factor2 is None:
            Factor2 = 0
        if Factor3 is None:
            Factor3 = 0
        return Factor1 + Factor2 + Factor3

result = R26_IC_NonExternalGuaranteeSpecialTransactionTypeSixtyMonsCheck()

